package MathExam_2;

import java.io.*;

public class Question {
    Question(){}
    File file = new File("题目.txt");
    String s = "";
    Random num = new Random();
    Random op = new Random();
    Student stu = new Student();
    float right = 0;
    public void Question1(int n,int stage,int grade){          //产生加减乘除（）
        for(int i=1;i<=n;i++){
            int key;
            int result;
            System.out.println("题目" + i + ":");
            int choice = num.RandomNum(4) + 1;               //随机产生运算符号的个数
            int count = 0;
            for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                int m = num.RandomNum(10);
                while(m==0)
                    m = num.RandomNum(10);
                s = s + m;                      //产生一个数字
                if (count < choice) {
                    s = s + " " + op.RandomChar(stage) + " ";             //在原基础上再产生字符
                    count++;
                }
            }                                              //产生基础题目
            if(grade>=3){                                  //产生括号
                s = op.RandomBranket(s,choice);
            }
            key = Key.Key(s);
            while(key==-1){
                choice = num.RandomNum(4) + 1;               //随机产生运算符号的个数
                count = 0;
                for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                    int m = num.RandomNum(10);
                    while(m==0)
                        m = num.RandomNum(10);
                    s = s + m;                      //产生一个数字
                    if (count < choice) {
                        s = s + " " + op.RandomChar(stage) + " ";             //在原基础上再产生字符
                        count++;
                    }
                }                                              //产生基础题目
                if(grade>=3){                                  //产生括号
                    s = op.RandomBranket(s,choice);
                }
            }
            try{                            //写入文件
                OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"gbk");
                BufferedWriter writer = new BufferedWriter(write);
                writer.write(s);
                writer.write(" = ");
                writer.close();
            }catch (IOException e){}
            try{                           //读取文件
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
                BufferedReader reader = new BufferedReader(read);
                System.out.print(reader.readLine());
                read.close();
            }catch (IOException e){}
            result = stu.Result();
            if(result==key){
                System.out.println("正确！");
                right++;
            }
            else
                System.out.println("错误，正确答案为：" + key);
            s = "";
        }
        System.out.println("完成" + n + "道题目,正确率为" + String.format("%.2f", right / n * 100) + "%");
    }
    public void Question2(int n,int stage,int grade) {          //产生真分数
        String k = "";
        String r = "";
        for(int i=1;i<=n;i++) {
            System.out.println("题目" + i + ":");
            int choice = op.RandomNum(3) + 1;               //随机产生运算符号的个数
            int count = 0;
            String fraction = "";
            for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                int a = num.RandomNum(10) + 1;
                int b = num.RandomNum(10) + 1;
                int gcd = GCD(a, b);
                if (gcd == a) fraction = "1";            //分子分母相同
                else {
                    if (gcd != 1) {                         //可约分
                        a = a / gcd;
                        b = b / gcd;
                    }
                    if (a < b) fraction = a + "/" + b;
                    else fraction = b + "/" + a;
                }
                s = s + fraction;                      //产生十以内的个数字
                if (count < choice) {
                    s = s + " " + op.RandomChar(4) + " ";             //在原基础上再产生字符
                    count++;
                }
            }
            s = op.RandomBranket(s,choice);
            k = Key.Key1(s);
            while(k.equals("")){
                choice = op.RandomNum(3) + 1;               //随机产生运算符号的个数
                count = 0;
                fraction = "";
                for (int j = 1; j <= choice + 1; j++) {        //产生题目，一共应该2*choice+1个元素
                    int a = num.RandomNum(10) + 1;
                    int b = num.RandomNum(10) + 1;
                    int gcd = GCD(a, b);
                    if (gcd == a) fraction = "1";            //分子分母相同
                    else {
                        if (gcd != 1) {                         //可约分
                            a = a / gcd;
                            b = b / gcd;
                        }
                        if (a < b) fraction = a + "/" + b;
                        else fraction = b + "/" + a;
                    }
                    s = s + fraction;                      //产生十以内的个数字
                    if (count < choice) {
                        s = s + " " + op.RandomChar(4) + " ";             //在原基础上再产生字符
                        count++;
                    }
                }
                s = op.RandomBranket(s,choice);
                k = Key.Key1(s);
            }
            try{                            //写入文件
                OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file),"gbk");
                BufferedWriter writer = new BufferedWriter(write);
                writer.write(s);
                writer.write(" = ");
                writer.close();
            }catch (IOException e){}
            try{                           //读取文件
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
                BufferedReader reader = new BufferedReader(read);
                System.out.print(reader.readLine());
                read.close();
            }catch (IOException e){}
            r = stu.Result1();
            if(k.equals(r)){
                System.out.println("正确！");
                right++;
            }
            else
                System.out.println("错误，正确答案为：" + k);
            s = "";
        }
        System.out.println("完成" + n + "道题目,正确率为" + String.format("%.2f", right / n * 100) + "%");
    }
    public static int GCD(int a,int b){
        int m = a;
        int n = b;
        while (m != n) {
            if (m > n)
                m -= n;
            else
                n -= m;
        }
        return m;
    }
}
